/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.opensocial.client.features {

import flash.events.Event;

import org.opensocial.client.base.ResponseItem;

/**
 * Event used for asynchronous OpenSocial data requests.
 * It is dispatched by the <code>AsyncDataRequest</code> object and carries the 
 * <code>ResponseItem</code> instance wrapped from response data object in javascript.
 * 
 * @see AsyncDataRequest
 * @see org.opensocial.client.base.ResponseItem
 *
 * @author chaowang@google.com (Jacky Wang)
 * @author yiziwu@google.com (Yizi Wu)
 */
public class ResponseItemEvent extends AsyncRequestEvent {

  /**
   * For the event dispatched when the OpenSocial data request get responsed.
   * @eventType complete
   */
  public static const COMPLETE:String = "complete";


  /**
   * For the event dispatched when the OpenSocial data request get error.
   * @eventType error
   */
  public static const ERROR:String = "error";


  /**
   * Constructor of the event.
   * @param responseItem The response data of <code>ResponseItem</code> type for this event.
   */
  public function ResponseItemEvent(responseItem:ResponseItem) {
    var type:String = ResponseItemEvent.COMPLETE;
    if (responseItem.hadError()) {
      type = ResponseItemEvent.ERROR;
    }
    super(type, responseItem);
  }


  /**
   * Gets the parsed response data of this event.
   * @return The parsed data object.
   */
  final public function get response():ResponseItem {
    return rawData_ as ResponseItem;
  }


  /**
   * Checks if the parsed data had error.
   * @return True if it had error.
   */
  final public function get hadError():Boolean {
    return (rawData_ as ResponseItem).hadError();
  }


  /**
   * Clones the event to a new one.
   * @return A new <code>ResponseItemEvent</code> object.
   */
  override public function clone():Event {
    return new ResponseItemEvent(response);
  }

}
}
